package com.haierp.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.haierp.service.IItemService;
import com.haierp.util.DimensionCodeUtil;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.haierp.mapper.ActivityDetailMapper;
import com.haierp.model.activity.Activity;
import com.haierp.model.activity.ActivityDetail;
import com.haierp.service.IActivityService;
import com.haierp.service.ISequenceUtilService;
import com.haierp.util.DateUtil;
import com.haierp.util.JsonPageResult;
import com.haierp.util.JsonResult;
import com.haierp.vo.ActivityQueryVO;
import com.wangzhixuan.commons.base.BaseController;
import com.wangzhixuan.commons.shiro.ShiroUser;
import com.wangzhixuan.commons.utils.HaiJsonUtils;

/**
 * 
 * @author wangkan
 *
 */
@Controller
@RequestMapping("/activity")
public class ActivityController extends BaseController {

	@Autowired
	private IActivityService activityService;
	@Autowired
	private ISequenceUtilService sequenceUtilService;
	@Autowired
	private	ActivityDetailMapper activityDetailMapper;
	
	/**
	 * 增加主题
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Object add(Activity activity) {
		JsonResult<String> result = new JsonResult<>();
		if(activity.getId()==null){
			activity.setGmtCreate(new Date());
			activity.setGmtModify(new Date());
			ShiroUser shiroUser = this.getShiroUser();
			activity.setActivityCreate(shiroUser.getLoginName());
			activity.setCompanyId(shiroUser.getCompanyId());//所属公司
			activity.setActivityNo("A"+String.format("%0"+2+"d",shiroUser.getId())+"D"+DateUtil.formatDate(new Date(), DateUtil.DATE_PARTEN_YYMMDDHHMMSS)+sequenceUtilService.gainORDSequence());//系统自动生成
			//主题详情
			String activityDetailList = activity.getActivityDetailsList();
			if (StringUtils.isNotBlank(activityDetailList)) {
				String s = activityDetailList.replace("&quot;", "\"");
				List<ActivityDetail> activityDetails = HaiJsonUtils.toBean(s, new TypeReference<List<ActivityDetail>>(){});
				activity.setActivityDetails(activityDetails);
				if(CollectionUtils.isEmpty(activityDetails)){
					return JsonResult.buildFailed("没有活动商品信息");
				}
			}
			//创建主题和主题详情
			activityService.addActivity(activity);

			result.buildIsSuccess(true);
		}else{
			result.buildData("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}
	
	
	/**
	 * 更新主题
	 * 
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Object update(Activity activity) {
		JsonResult<String> result = new JsonResult<>();
		if(activity.getId()!=null) {
			activity.setGmtModify(new Date());
			ShiroUser shiroUser = this.getShiroUser();
			activity.setCompanyId(shiroUser.getCompanyId());
			String activityDetailList = activity.getActivityDetailsList();
			if (StringUtils.isNotBlank(activityDetailList)) {
				String s = activityDetailList.replace("&quot;", "\"");
				List<ActivityDetail> activityDetails = HaiJsonUtils.toBean(s, new TypeReference<List<ActivityDetail>>(){});
				activity.setActivityDetails(activityDetails);
			}
		    activityService.updateActivity(activity);
			result.buildIsSuccess(true);
		} else {
			result.buildData("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}
	
	/**
	 * 查询单个主题
	 */
	@RequestMapping("/query")
	@ResponseBody
	public Object query(Long id) {
		JsonResult<Activity> result = new JsonResult<>();
		Activity activity = activityService.queryActivity(id);
		return result.buildData(activity).buildIsSuccess(true);
	}
	
	/**
	 * 批量查询主题活动
	 * @return
	 */
	@RequestMapping("/queryActivityList")
	@ResponseBody
	public Object queryactivityList(ActivityQueryVO activityQueryVO) {
		if(activityQueryVO.getActivityEnd()!=null) {
			String endGmtCreateStr = DateUtil.ymdFormat(activityQueryVO.getActivityEnd());
			Date endGmtCreate = DateUtil.parseDate(endGmtCreateStr + " 23:59:59");
			activityQueryVO.setActivityEnd(endGmtCreate);
		}
		JsonPageResult<List<Activity>> result = activityService.queryOuterOrderList(activityQueryVO);
		return result.buildIsSuccess(true);
	}
	
	/**
	 * 删除单个活动主题
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(Long id) {
		JsonResult<String> result = new JsonResult<>();
		List<Long> idList = new ArrayList<Long>();
		try {
			Activity activity = activityService.selectById(id);
			if(activity==null){
				return JsonResult.buildFailed("删除主题活动不存在！");
			}
			activityService.deleteSelective(activity);
			EntityWrapper<ActivityDetail> entityWrapper = new EntityWrapper<ActivityDetail>();
			entityWrapper.where("activity_id = {0}", id);
			List<ActivityDetail> activityDetaillist = activityDetailMapper.selectList(entityWrapper);
			for (ActivityDetail activityDetail : activityDetaillist) {
				idList.add(activityDetail.getId());
			}
			if(idList.size()>0){
				activityDetailMapper.deleteBatchIds(idList);
			}
		} catch(Exception e) {
			return result.buildIsSuccess(false).buildMsg(e.getMessage());
		}
		return result.buildIsSuccess(true);
	}

	/**
	 * 生成商品活动的二维码
	 */
	@RequestMapping("/createActivityItemDimensioncode")
	@ResponseBody
	public Object createActivityItemDimensioncode(Long itemId,Long activityId) {
		JsonResult<String> result = new JsonResult<>();
		result = activityService.createActivityItemDimensioncode(itemId,activityId);
		return result;
	}
}
